<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2011-12-20 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

abstract class BaseModule
{
	var $config = array();
	var $request = array();
	var $response = array();
	
	var $sid = '';
	
	var $act = '';
	var $user = array();
	
	function __construct($request)
	{
		load_setting('config_global');
		
		$this->initSession($request);
		
		$this->act = $request['act'];
		
		$request['page_size'] = isset($request['page_size']) ? min(MAX_PER_PAGE, max(0, (int)$request['page_size'])) : DEFAULT_PER_PAGE;
		$request['page_index'] = isset($request['page_index']) ? max(0, (int)$request['page_index'] - 1) : 0;
		$request['page_index'] = $request['page_index'] * $request['page_size'];
		$this->request = $request;
		
		$this->_checkUser();
		
		$this->response['status'] = STATUS_S;
		
		$this->updateSession();
	}
	
	function message($code, $status)
	{
		show_msg($code, $status);
	}
	
	function showResponse()
	{
		@header('User-Token: '.$this->sid, false);
		
		$json = '';
		if(is_array($this->response) && count($this->response) > 0)
		{
			//if (isset($this->response['create_time']))
			//{ 
			//	$this->response['create_time'] = format_time($this->response['create_time']);
			//}
			
			if (isset($this->response['total_count']) && $this->response['total_count'] > 0)
			{
				$this->response['total_page'] = ceil($this->response['total_count'] / $this->request['page_size']);
				$this->response['page_index'] = ($this->request['page_index'] / $this->request['page_size'])+1;
				$this->response['page_size'] = $this->request['page_size'];
				unset($this->response['total_count']);
			}
			
			//echo "12--- ".microtime()." ---\r\n";
			
			$result = array_merge(array('version' => API_VERSION), $this->response);
			$json = @json_encode($result);
			
			//echo "13--- ".microtime()." ---\r\n";
		}
		
		requestLog($this->request, (string)$json, $this->response['status']);

		//echo "14--- ".microtime()." ---\r\n";
		
		echo $json;
		exit();
	}

	function initSession($request)
	{
		if ($request['sid'] != '') {
			session_id($request['sid']);
			$this->sid = $request['sid'];
			session_start();
		} else {
			session_start();
			$this->sid = session_id();
		}
	}
	
	function getSession()
	{
		return isset($_SESSION['user']) ? $_SESSION['user'] : array();
	}
	
	function updateSession()
	{
		$_SESSION['user'] = $this->user;
	}
	
	function _checkUser()
	{
		if ($this->act == 'register' || $this->act == 'login') return;
		
		$this->user = $this->getSession();
		
		if(!isset($this->user['id']) || $this->user['id'] < 1)
		{
			$this->message('NEED_LOGIN', STATUS_E);
		}
	}
	
	function checkRequest($reqFile)
	{
		require ROOT_PATH.'mobiapi/request/'.$reqFile.'.req.php';
		
		if (class_exists('MobiRequest')) {
			$req = new MobiRequest($this->request);
			if (!$req->is_valid()) $this->message('BAD_REQUEST', STATUS_E);
			
			$this->request = object_to_array($req);
		}
	}
	
	function getResponse($respFile, $response)
	{
		require ROOT_PATH.'mobiapi/response/'.$respFile.'.resp.php';
				
		if (class_exists('MobiResponse')) {
			$resp = new MobiResponse($response);
			
			return object_to_array($resp);
		}
		
		return null;
	}
	
	abstract function excute();
}